import { defineStore } from 'pinia'
import { ElMessage, ElMessageBox } from 'element-plus'
import $ajax from '@/api/to8080.js'
import { alertCode } from '@/utils/msg.js'

export const useRoles = defineStore('role', {
  state: () => ({
    /** @type {{ id:number, rname:String, nbl:number, remark:String }[]} */
    roles: [],
    /** @type {{ text: string, id: number, isFinished: boolean }[]}*/
    curRole: {
      id: 3,
      rname: '管理员',
      nbl: 1,
      remark: '管理其他人'
    },
    curIndex: 1,
    switch: true,
    dialogFormVisible: false,
    msg: '',
    form: {
      id: -1,
      rname: '角色',
      remark: '备注',
      nbl: 1
    },
    treeRef: [],
    drawerVisible: false,
    treeData: [],
    confirmDialog: false,
    isDeleted: false
  }),
  getters: {},
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    async loadData() {
      if (this.roles === null || this.roles.length === 0) {
        const path = '/role/list'
        const res = await $ajax.get(path)
        if (res.code === 200) {
          this.roles = res.data
        }
        console.log(res)
      }
    },
    async handleNblSwitch(index, row) {
      this.curIndex = index
      this.curRole = row
      const path = '/role/edit'
      const res = await $ajax.put(path, this.curRole)
      this.msg = res.msg
    },
    async handleDelete(index, row) {
      this.roles.splice(index, 1)
      this.form.id = row.id
      const path = '/role/delete'

      const res = await $ajax.post(path, this.form)
    },
    async handleAdd() {
      this.dialogFormVisible = true
      this.form = { id: -1, rname: '', remark: '' }
    },
    async handleEdit(index, row) {
      this.dialogFormVisible = true

      this.form = JSON.parse(JSON.stringify(row))

      this.curIndex = index
    },
    save() {
      if (this.form.id === -1) {
        this.addData()
      } else {
        this.editData()
      }
    },
    closeDialog() {
      this.dialogFormVisible = false
    },
    closeDrawer() {
      this.drawerVisible = false
    },
    closeConfirm() {
      this.confirmDialog = false
    },
    openConfirm() {
      this.confirmDialog = true
    },
    confirmDelete(index, row) {
      this.isDelete = true
      this.handleDelete(index, row)
    },
    initParams() {
      this.isDeleted = false
      this.drawerVisible = false
      this.confirmDialog = false
      this.dialogFormVisible = false
    },
    async addData() {
      const path = '/role/add'
      const res = await $ajax.post(path, this.form)
      if (res.code === 200) {
        console.log(res.data)
        this.form.id = res.data
        this.form.nbl = 1
        const temp = JSON.parse(JSON.stringify(this.form))
        this.roles.push(temp)
        this.closeDialog()
        alertCode(200)
      } else {
        alertCode(400)
      }
    },
    async editData() {
      const path = '/role/edit'
      const res = await $ajax.put(path, this.form)
      console.log(res)
      if (res.code === 200) {
        this.roles[this.curIndex] = this.form

        this.closeDialog()

        alertCode(200)
      } else {
        alertCode(400)
      }
    },
    async confirmClick(sids) {
      const path = '/role/sysnode/auth/' + this.curRole.id

      const res = await $ajax.put(path, sids)

      setTimeout(() => {
        this.drawerVisible = false
      }, 1000) // 等待1秒
      alertCode(200)
    },
    showTree() {
      this.drawerVisible = true
    },
    async setCheckedKeys(row) {
      this.curRole = row
      const path = '/role/sysnode/load/' + this.curRole.id
      const res = await $ajax.get(path)
      console.log(res.data)
      return res.data
    },
    async loadTree() {
      const path = '/sysnode/select/all/tree'

      const res = await $ajax.get(path)

      const arr = []
      arr.push(res.data)
      this.treeData = arr
      console.log(this.treeData)
    }
  }
})
